﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Public;
namespace DAL
{
    public class NhomKHNCC_DAL
    {
        Connection conn = new Connection();

        #region "Lay danh sach Nhom khach hang, nha cung cap"
        public DataTable LayDSKHNCC()
        {
            return conn.LoadData("select * from NHOM");
        }
        #endregion

        #region "Them Nhom khach hang, nha cung cap"
        public int  ThemSuaDSKHNCC(NhomKHNCC_PB nhom_pb)
        {
            int nparameter = 4;
            string []name = new string [nparameter];
            object[] value = new object[nparameter];

            name[0] = "@manhom"; value[0] = NhomKHNCC_PB.MaNhom;
            name[1] = "@tennhom"; value[1] = NhomKHNCC_PB.TenNhom;
            name[2] = "@diengiai"; value[2] = NhomKHNCC_PB.DienGiai;
            name[3] = "@loai"; value[3] = NhomKHNCC_PB.Loai;
            
            string sql = @"if exists (select * from NHOM where [MANHOM] = @manhom)
                                UPDATE [NHOM]
                                    SET
                                        [TENNHOM]    = @tennhom
                                        ,[DIENGIAI]   = @diengiai
                                        ,[LOAI]       = @loai
                                    WHERE  MANHOM		= @manhom 
                                else
                                INSERT INTO [NHOM]
                                            ([MANHOM]
                                            ,[TENNHOM]
                                            ,[DIENGIAI]
                                            ,[LOAI])
                                VALUES (@manhom, @tennhom, @diengiai,@loai)
                            ";
            return conn.Update(sql,name,value,nparameter);
        }
        #endregion

        #region "Xoa Nhom khach hang, nha cung cap"
        public int XoaDSKHNCC(NhomKHNCC_PB nhom_pb)
        {
            int nparameter = 4;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];

            name[0] = "@manhom"; value[0] = NhomKHNCC_PB.MaNhom;
            name[1] = "@tennhom"; value[1] = NhomKHNCC_PB.TenNhom;
            name[2] = "@diengiai"; value[2] = NhomKHNCC_PB.DienGiai;
            name[3] = "@loai"; value[3] = NhomKHNCC_PB.Loai;
            string sql = "delete from NHOM  where MANHOM = @manhom";
            return conn.Update(sql, name, value, nparameter);
        }

        public int XoaDSKHNCC(string ma)
        {
            int nparameter = 1;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];

            name[0] = "@manhom"; value[0] = ma;
            string sql = "delete from NHOM  where MANHOM = @manhom";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion
        public bool Exists(string ma)
        {
            string sql = string.Format("select * from NHOM where MANHOM = '{0}'", ma);
            DataTable dt = conn.LoadData(sql);
            if (dt == null) return false;

            return dt.Rows.Count > 0;
        }
    }
}
